package com.panda.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.panda.common.utils.DateUtils;
import com.panda.common.utils.DictUtils;
import com.panda.order.domain.TPandaOrderBuyer;
import com.panda.order.domain.TPandaOrderHead;
import com.panda.order.domain.TPandaOrderLine;
import com.panda.order.mapper.TPandaOrderHeadMapper;
import com.panda.order.service.ITPandaOrderBuyerService;
import com.panda.order.service.ITPandaOrderHeadService;
import com.panda.order.service.ITPandaOrderLineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 淘宝订单信息Service业务层处理
 * 
 * @author panda
 * @date 2021-03-14
 */
@Service
public class TPandaOrderHeadServiceImpl implements ITPandaOrderHeadService 
{
    private static final Logger log = LoggerFactory.getLogger(TPandaOrderHeadServiceImpl.class);
    @Autowired
    private TPandaOrderHeadMapper tPandaOrderHeadMapper;

    @Autowired
    private ITPandaOrderBuyerService orderBuyerService;
    @Autowired
    private ITPandaOrderLineService orderLineService;

    /**
     * 查询淘宝订单信息
     * 
     * @param id 淘宝订单信息ID
     * @return 淘宝订单信息
     */
    @Override
    public TPandaOrderHead selectTPandaOrderHeadById(Long id)
    {
        return tPandaOrderHeadMapper.selectTPandaOrderHeadById(id);
    }

    /**
     * 查询淘宝订单信息列表
     * 
     * @param tPandaOrderHead 淘宝订单信息
     * @return 淘宝订单信息
     */
    @Override
    public List<TPandaOrderHead> selectTPandaOrderHeadList(TPandaOrderHead tPandaOrderHead)
    {
        List<TPandaOrderHead> orderHeads = tPandaOrderHeadMapper.selectTPandaOrderHeadList(tPandaOrderHead);
        for(TPandaOrderHead head:orderHeads){
            // 翻译
            head.setStatusShow(DictUtils.getDictLabel("taobao_order_status", head.getStatus()));
            head.setTypeShow(DictUtils.getDictLabel("taobao_order_type", head.getType()));

            // 商品名
            List<TPandaOrderLine> orders = head.getOrders();
            if(!CollectionUtils.isEmpty(orders)){
                head.setProductName(orders.get(0).getTitle());
            }
        }

        return orderHeads;
    }

    /**
     * 新增淘宝订单信息
     * 
     * @param tPandaOrderHead 淘宝订单信息
     * @return 结果
     */
    @Override
    public int insertTPandaOrderHead(TPandaOrderHead tPandaOrderHead)
    {
        tPandaOrderHead.setCreateTime(DateUtils.getNowDate());
        return tPandaOrderHeadMapper.insertTPandaOrderHead(tPandaOrderHead);
    }

    /**
     * 修改淘宝订单信息
     * 
     * @param tPandaOrderHead 淘宝订单信息
     * @return 结果
     */
    @Override
    public int updateTPandaOrderHead(TPandaOrderHead tPandaOrderHead)
    {
        tPandaOrderHead.setUpdateTime(DateUtils.getNowDate());
        return tPandaOrderHeadMapper.updateTPandaOrderHead(tPandaOrderHead);
    }

    /**
     * 批量删除淘宝订单信息
     * 
     * @param ids 需要删除的淘宝订单信息ID
     * @return 结果
     */
    @Override
    public int deleteTPandaOrderHeadByIds(Long[] ids)
    {
        return tPandaOrderHeadMapper.deleteTPandaOrderHeadByIds(ids);
    }

    /**
     * 删除淘宝订单信息信息
     * 
     * @param id 淘宝订单信息ID
     * @return 结果
     */
    @Override
    public int deleteTPandaOrderHeadById(Long id)
    {
        return tPandaOrderHeadMapper.deleteTPandaOrderHeadById(id);
    }

    @Override
    public void saveTbOrderList(List<TPandaOrderHead> orderHeads) {
        if (CollectionUtils.isEmpty(orderHeads)) {
           return;
        }
        List<String> orderTids = orderHeads.stream().map(TPandaOrderHead::getTid).collect(Collectors.toList());
        Map<String,TPandaOrderHead> oldOrderMap = new HashMap<>();
        if(!CollectionUtils.isEmpty(orderTids)){
            // 查询已入库订单
            List<TPandaOrderHead> oldOrders = tPandaOrderHeadMapper.queryOrderListByTids(orderTids);
            log.info("已入库订单:{}", JSON.toJSONString(oldOrders));
            oldOrders.forEach(item->oldOrderMap.put(item.getTid(),item));
        }

        for(TPandaOrderHead orderHead: orderHeads){
            TPandaOrderHead orderHeadOld = oldOrderMap.get(orderHead.getTid());
            if(orderHeadOld == null){
                log.info("新增：{}",orderHead.getTid());
                // 新增
                tPandaOrderHeadMapper.insertTPandaOrderHead(orderHead);
                List<TPandaOrderLine> orders = orderHead.getOrders();
                for(TPandaOrderLine line:orders){
                    line.setTid(orderHead.getTid());
                    orderLineService.insertTPandaOrderLine(line);
                }
                TPandaOrderBuyer orderBuyer = orderHead.getOrderBuyer();
                orderBuyer.setTid(orderHead.getTid());
                orderBuyerService.insertTPandaOrderBuyer(orderBuyer);
            }else {
                log.info("更新：{}",orderHead.getTid());
                // 更新
                orderHead.setId(orderHeadOld.getId());
                tPandaOrderHeadMapper.updateTPandaOrderHead(orderHead);
                List<TPandaOrderLine> orders = orderHead.getOrders();
                List<TPandaOrderLine> oldOrderLines = orderHeadOld.getOrders();
                // 删除原订单行
                orderLineService.deleteTPandaOrderLineByTid(orderHeadOld.getTid());
                orderBuyerService.deleteTPandaOrderBuyerByTid(orderHeadOld.getTid());

                for(TPandaOrderLine line:orders){
                    line.setTid(orderHead.getTid());
                    orderLineService.insertTPandaOrderLine(line);
                }
                TPandaOrderBuyer orderBuyer = orderHead.getOrderBuyer();
                orderBuyer.setTid(orderHead.getTid());
                TPandaOrderBuyer orderBuyerOld = orderHeadOld.getOrderBuyer();
                if(orderBuyerOld!=null&&orderBuyerOld.getId()!=null){
                    orderBuyer.setId(orderBuyerOld.getId());
                    orderBuyerService.updateTPandaOrderBuyer(orderBuyer);
                }
            }
        }

    }

    @Override
    public List<TPandaOrderHead> queryTbOrderListByStartTime(Date startTime) {
        return tPandaOrderHeadMapper.queryTbOrderListByStartTime(startTime);
    }
}
